package com.course.utils;

import android.graphics.Bitmap;

import com.course.utils.binary.Base64;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class ImageUtil {


    public static String imageToBase64ByLocal(String imgFile) {
        // 将图片文件转化为字节数组字符串，并对其进行Base64编码处理
        InputStream in = null;
        byte[] data = null;
        // 读取图片字节数组
        try {
            in = new FileInputStream(imgFile);
            data = new byte[in.available()];
            in.read(data);
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return new Base64().encodeAsString(data);// 返回Base64编码过的字节数组字符串
    }


    public static String bitmapToBase64(Bitmap bitmap) {
        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
        byte[] byteArray = byteArrayOutputStream.toByteArray();
        return new Base64().encodeAsString(byteArray);
    }



}
